home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1LA89G0 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  4.6 KB  |  153 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Enumeration;
  5. import java.util.Hashtable;
  6.  
  7. public class SimpleAttributeSet implements MutableAttributeSet, Serializable {
  8.    public static final AttributeSet EMPTY = new SimpleAttributeSet();
  9.    private Hashtable table = new Hashtable(3);
  10.  
  11.    public SimpleAttributeSet() {
  12.    }
  13.  
  14.    public SimpleAttributeSet(AttributeSet source) {
  15.       this.addAttributes(source);
  16.    }
  17.  
  18.    private SimpleAttributeSet(Hashtable table) {
  19.       this.table = table;
  20.    }
  21.  
  22.    public void addAttribute(Object name, Object value) {
  23.       this.table.put(name, value);
  24.    }
  25.  
  26.    public void addAttributes(AttributeSet attributes) {
  27.       Enumeration names = attributes.getAttributeNames();
  28.  
  29.       while(names.hasMoreElements()) {
  30.          Object name = names.nextElement();
  31.          this.addAttribute(name, attributes.getAttribute(name));
  32.       }
  33.  
  34.    }
  35.  
  36.    public Object clone() {
  37.       return new SimpleAttributeSet((Hashtable)this.table.clone());
  38.    }
  39.  
  40.    public boolean containsAttribute(Object name, Object value) {
  41.       return value.equals(this.getAttribute(name));
  42.    }
  43.  
  44.    public boolean containsAttributes(AttributeSet attributes) {
  45.       boolean result = true;
  46.  
  47.       Object name;
  48.       for(Enumeration names = attributes.getAttributeNames(); result && names.hasMoreElements(); result = attributes.getAttribute(name).equals(this.getAttribute(name))) {
  49.          name = names.nextElement();
  50.       }
  51.  
  52.       return result;
  53.    }
  54.  
  55.    public AttributeSet copyAttributes() {
  56.       return (AttributeSet)this.clone();
  57.    }
  58.  
  59.    public boolean equals(Object obj) {
  60.       if (obj instanceof AttributeSet) {
  61.          AttributeSet attrs = (AttributeSet)obj;
  62.          return this.isEqual(attrs);
  63.       } else {
  64.          return false;
  65.       }
  66.    }
  67.  
  68.    public Object getAttribute(Object name) {
  69.       Object value = this.table.get(name);
  70.       if (value == null) {
  71.          AttributeSet parent = this.getResolveParent();
  72.          if (parent != null) {
  73.             value = parent.getAttribute(name);
  74.          }
  75.       }
  76.  
  77.       return value;
  78.    }
  79.  
  80.    public int getAttributeCount() {
  81.       return this.table.size();
  82.    }
  83.  
  84.    public Enumeration getAttributeNames() {
  85.       return this.table.keys();
  86.    }
  87.  
  88.    public AttributeSet getResolveParent() {
  89.       return (AttributeSet)this.table.get(StyleConstants.ResolveAttribute);
  90.    }
  91.  
  92.    public int hashCode() {
  93.       return this.table.hashCode();
  94.    }
  95.  
  96.    public boolean isDefined(Object attrName) {
  97.       return this.table.containsKey(attrName);
  98.    }
  99.  
  100.    public boolean isEmpty() {
  101.       return this.table.isEmpty();
  102.    }
  103.  
  104.    public boolean isEqual(AttributeSet attr) {
  105.       return this.getAttributeCount() == attr.getAttributeCount() && this.containsAttributes(attr);
  106.    }
  107.  
  108.    public void removeAttribute(Object name) {
  109.       this.table.remove(name);
  110.    }
  111.  
  112.    public void removeAttributes(AttributeSet attributes) {
  113.       Enumeration names = attributes.getAttributeNames();
  114.  
  115.       while(names.hasMoreElements()) {
  116.          Object name = names.nextElement();
  117.          Object value = attributes.getAttribute(name);
  118.          if (value.equals(this.getAttribute(name))) {
  119.             this.removeAttribute(name);
  120.          }
  121.       }
  122.  
  123.    }
  124.  
  125.    public void removeAttributes(Enumeration names) {
  126.       while(names.hasMoreElements()) {
  127.          this.removeAttribute(names.nextElement());
  128.       }
  129.  
  130.    }
  131.  
  132.    public void setResolveParent(AttributeSet parent) {
  133.       this.addAttribute(StyleConstants.ResolveAttribute, parent);
  134.    }
  135.  
  136.    public String toString() {
  137.       String s = "";
  138.       Enumeration names = this.getAttributeNames();
  139.  
  140.       while(names.hasMoreElements()) {
  141.          Object key = names.nextElement();
  142.          Object value = this.getAttribute(key);
  143.          if (value instanceof AttributeSet) {
  144.             s = s + key + "=**AttributeSet** ";
  145.          } else {
  146.             s = s + key + "=" + value + " ";
  147.          }
  148.       }
  149.  
  150.       return s;
  151.    }
  152. }
  153.